/*
 * MainPanel.java
 *
 * Created on 17.11.2008., 15.35
 */

package rs.fon.rapidminer.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;

import javax.swing.WindowConstants;

import rs.fon.rapidminer.gui.extended.ProcessPreviewPanel;
import rs.fon.rapidminer.gui.panels.navigator.Navigator;
import rs.fon.rapidminer.process.Process;
import rs.fon.rapidminer.process.Step;
import rs.fon.rapidminer.process.builder.ProcessBuilder;

/**
 * 
 * @author Sasa
 */

public class MainPanel extends javax.swing.JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** Navogator instance **/
	private static Navigator navigator = new Navigator();

	/** Toolbar instance **/
	private static rs.fon.rapidminer.gui.panels.toolbar.Toolbar toolbar = new rs.fon.rapidminer.gui.panels.toolbar.Toolbar();

	/** Editor instance **/
	private static rs.fon.rapidminer.gui.editor.Editor editor = new rs.fon.rapidminer.gui.editor.Editor();

	private static ProcessPreviewPanel tree = new ProcessPreviewPanel();

	/** Process instance that is used across the panels **/
	private static rs.fon.rapidminer.process.Process process;

	/**
	 * Creates new form MainPanel. This form is draw according to the provided
	 * process builder.
	 */
	public MainPanel(ProcessBuilder builder) {
		process = builder.buildProces();
		drawPanels();
		fillPanels();
		setFrameCentered();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 461,
				Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 372,
				Short.MAX_VALUE));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void drawPanels() {
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

		setLayout(new BorderLayout());

		add(toolbar, BorderLayout.NORTH);

		add(navigator, BorderLayout.WEST);

		add(editor, BorderLayout.CENTER);

		add(tree, BorderLayout.EAST);

		pack();
	}

	/**
	 * Fills required data upon startup of the panel.
	 */
	private void fillPanels() {
		navigator.load(process.getProcessSteps());
		refreshTree();
	}

	public static Process getProcess() {
		return process;
	}

	public static void setProcess(Process process) {
		MainPanel.process = process;
		navigator.load(process.getProcessSteps());
		editor.restart();
		refreshTree();

	}

	public static void refreshStep(Step step) {
		editor.load(step);
	}

	public static void refreshTree() {
		tree.refreshTreePreview(process);
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	// End of variables declaration//GEN-END:variables

	/**
	 * This method position frame to the center of the screen
	 * 
	 */
	public void setFrameCentered() {
		Toolkit t = Toolkit.getDefaultToolkit();

		Dimension screenSize = t.getScreenSize();
		int width = screenSize.width;
		int height = screenSize.height;

		int x = (int) ((width / 2) - (this.getSize().getWidth() / 2));
		int y = (int) ((height / 2) - (this.getSize().getHeight() / 2));

		this.setLocation(new Point(x, y));
	}
}
